package com.zhong.gang.applib.base.dialog;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.annotation.LayoutRes;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.zhong.gang.applib.R;
import com.zhong.gang.applib.rx.RxManager;

/**
 * Created by ajin on 2018/7/22.
 * 证件选择的popw
 */

public abstract class BasePop<T extends ViewDataBinding> extends PopupWindow  {
    private View pop_View;
    private AnimatorSet animatorSet;
    public T bind;

    private RxManager rxManager;
    public Context context;
    public RxManager getRxManager() {
        if (rxManager == null)
            rxManager = new RxManager();
        return rxManager;
    }
    /***
     * 初始化数据
     * @param context
     */
    public BasePop(Context context) {
        super(context);
        setFocusable(true);
        setOutsideTouchable(true);
        this.context = context;
        pop_View = LayoutInflater.from(context).inflate(LayoutRes(),null);
        try {
            bind = DataBindingUtil.bind(pop_View);
        }catch (Exception e){}
        setContentView(pop_View);
        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.transparent)));
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        initView();
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        Anmaion();
    }


    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        Anmaion();
    }

    /**下滑动画*/
    private void Anmaion(){
        pop_View.setVisibility(View.VISIBLE);
        if (animatorSet == null)
            animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(pop_View, "translationY", -300, 0).setDuration(400),
                ObjectAnimator.ofFloat(pop_View, "alpha", 0, 1).setDuration(400 * 3 / 2));
        animatorSet.start();
    }


    @LayoutRes
    public abstract  int LayoutRes();
    public abstract  void initView();
}
